package visiopuce.graph;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.List;

import org.jfree.chart.axis.AxisState;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.data.Range;
import org.jfree.ui.RectangleEdge;

public class NumberAxis2 extends NumberAxis {
	private long min;

	private long max;

	public NumberAxis2() {
		this(null, -1, -1);
	}

	public NumberAxis2(String label, long min, long max) {
		super(label);
		this.min = min;
		this.max = max;
	}

	public NumberAxis2(String label) {
		this(label, -1, -1);
		this.min = -1;
		this.max = -1;
	}

	public void resizeRange(double percent, double anchorValue) {
		double borneMin;
		double borneMax;
		if (percent > 0.0) {
			double halfLength = getRange().getLength() * percent / 2;
			if (min != -1 && max != -1) {
				borneMin = Math.max(min, anchorValue - halfLength);
				borneMax = Math.min(max, anchorValue + halfLength);
			} else {
				borneMin = anchorValue - halfLength;
				borneMax = anchorValue + halfLength;
			}

			Range adjusted = new Range(borneMin, borneMax);
			setRange(adjusted);
		} else {
			setAutoRange(true);
		}
	}
	
	public AxisState draw(Graphics2D g2, double cursor, Rectangle2D plotArea,
			Rectangle2D dataArea, RectangleEdge edge,
			PlotRenderingInfo plotState) {

		AxisState state = null;
		// if the axis is not visible, don't draw it...
		if (!isVisible()) {
			state = new AxisState(cursor);
			// even though the axis is not visible, we need ticks for the
			// gridlines...
			List ticks = refreshTicks(g2, state, dataArea, edge);
			state.setTicks(ticks);
			return state;
		}

		// draw the tick marks and labels...
		state = drawTickMarksAndLabels(g2, cursor, plotArea, dataArea, edge);

		// // draw the marker band (if there is one)...
		// if (getMarkerBand() != null) {
		// if (edge == RectangleEdge.BOTTOM) {
		// cursor = cursor - getMarkerBand().getHeight(g2);
		// }
		// getMarkerBand().draw(g2, plotArea, dataArea, 0, cursor);
		// }

		// draw the axis label...
		state = drawLabel(getLabel(), g2, plotArea, dataArea, edge, state);
		createAndAddEntity(cursor, state, dataArea, edge, plotState);
		return state;

	}
}
